Page({
  data: {
    isDarkMode: false,
    locationText: '定位中...',
    bars: []
  },
  onShow() {
    const isDark = wx.getStorageSync('isDarkMode');
    this.setData({
      isDarkMode: typeof isDark === 'boolean' ? isDark : (getApp().globalData.isDarkMode || false)
    });
    this.getLocationAndBars();
  },
  refreshLocation() {
    this.getLocationAndBars();
  },
  getLocationAndBars() {
    wx.showLoading({ title: '定位中...' });
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        const { latitude, longitude } = res;
        this.setData({ locationText: `经纬度：${latitude.toFixed(4)}, ${longitude.toFixed(4)}` });
        // 模拟数据，实际开发时可替换为后端请求
        const mockBars = [
          {
            id: 1,
            name: '微醺酒馆·市中心店',
            address: '北京市朝阳区三里屯路8号',
            phone: '010-88886666',
            lat: latitude + 0.002,
            lng: longitude + 0.002,
            distance: 0.5
          },
          {
            id: 2,
            name: '香遇合作酒吧·望京',
            address: '北京市朝阳区望京SOHO塔2',
            phone: '010-12345678',
            lat: latitude - 0.003,
            lng: longitude + 0.001,
            distance: 1.2
          },
          {
            id: 3,
            name: '夜色微醺·合作店',
            address: '北京市东城区东直门外大街12号',
            phone: '010-23456789',
            lat: latitude + 0.001,
            lng: longitude - 0.002,
            distance: 2.1
          },
          {
            id: 4,
            name: '调香师酒吧',
            address: '北京市西城区西单北大街88号',
            phone: '010-87654321',
            lat: latitude - 0.002,
            lng: longitude - 0.003,
            distance: 3.5
          }
        ];
        this.setData({ bars: mockBars });
        wx.hideLoading();
      },
      fail: () => {
        wx.hideLoading();
        wx.showModal({
          title: '定位失败',
          content: '请在小程序设置中授权地理位置，以便显示附近合作酒吧。',
          confirmText: '去设置',
          cancelText: '取消',
          success: (res) => {
            if (res.confirm) {
              wx.openSetting();
            }
            this.setData({ locationText: '定位失败', bars: [] });
          }
        });
      }
    });
  },
  openMap(e) {
    const lat = Number(e.currentTarget.dataset.lat);
    const lng = Number(e.currentTarget.dataset.lng);
    wx.openLocation({ latitude: lat, longitude: lng, scale: 16 });
  },
  callBar(e) {
    const phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({ phoneNumber: phone });
  }
}); 